/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.blog.city.checkthedep.sessionbeans;

import com.blog.city.checkthedep.entities.Article;
import com.blog.city.checkthedep.entities.Comment;
import com.blog.city.checkthedep.entities.RegisteredUser;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Breath
 */
@Stateless
public class RegisteredUserFacade extends AbstractFacade<RegisteredUser> implements RegisteredUserFacadeLocal {

    @PersistenceContext(unitName = "com.blog.city_CheckTheDep_war_1.0-SNAPSHOTPU")
    private EntityManager em;

    /**
     * 
     * @return
     */
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    /**
     * 
     * @param em
     */
    protected void setEm(EntityManager em) {
        this.em = em;
    }

    /**
     * 
     */
    public RegisteredUserFacade() {
        super(RegisteredUser.class);
    }

    /**
     * 
     * @param username
     * @return
     */
    @Override
    public RegisteredUser findRegUserByUsername(String username) {
        try {
            return (RegisteredUser) this.getEntityManager().createQuery("SELECT r FROM RegisteredUser r WHERE r.username = :username").setParameter("username", username).getSingleResult();
        } catch (NoResultException e) {
            System.err.println("Caught NOResultException: "+  e.getMessage());
            return null;
        } catch (NonUniqueResultException e) {
            System.err.println("Caught NonUniqueResultException: "+  e.getMessage());
            return null;
        } catch (IllegalStateException e) {
            System.err.println("Caught IllegalStateException: "+  e.getMessage());
            return null;
        }
    }

    /**
     * This method is not used because we had problems with deserializing the object. Could not get back the actual list. The problem was also in
     * Arquillian tests.
     * @param author
     * @return
     */
    @Override
    public List<Article> findArticleByAuthorNotUsed(RegisteredUser author) {
        return (List<Article>)author.getArticleList();
    }

    /**
     * This method is not used because we had problems with deserializing the object. Could not get back the actual list. The problem was also in 
     * Arquillian tests.
     * @param author
     * @return
     */
    @Override
    public List<Comment> findCommentsByAuthorNotUsed(RegisteredUser author) {
        return (List<Comment>)author.getCommentList();
    }
}
